/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cetc.iframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import com.cetc.dao.Dao;

public class XiaoShouPaiHang extends javax.swing.JInternalFrame {

    /**
     * Creates new form XiaoShouPaiHang
     */
    public XiaoShouPaiHang() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();
        jMenu3 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        year = new javax.swing.JComboBox<>();
        month = new javax.swing.JComboBox<>();
        okButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        condition = new javax.swing.JComboBox<>();
        jLabel4 = new javax.swing.JLabel();
        operation = new javax.swing.JComboBox<>();

        jMenu1.setText("jMenu1");

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenu3.setText("jMenu3");

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("销售排行");
        setPreferredSize(new java.awt.Dimension(600, 300));

        jLabel1.setText("对");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                
            },
            new String [] {
            		"商品编号", "商品名称", "销售金额", "销售数量", "简称", "产地", "单位", "规格", "包装", "批号", "批准文号","简介","供应商"
            }
        ));
        jTable1.setRowHeight(24);
        jScrollPane1.setViewportView(jTable1);
        jTable1.setEnabled(false);// 设置表格模型无法响应用户输入
        
        okButton.setText("确定");
        
        okButton.addActionListener(new OkAction());// 点击“确定”按钮后，发生的事件
        
        for (int i = 1981, j = 0; i <= date.get(Calendar.YEAR) + 1; i++, j++) {// 利用循环，向“年”下拉列表中添加1981~(当前时间年份+1)选项
			year.addItem(i);
			if (i == date.get(Calendar.YEAR))// 当前时间年份
				year.setSelectedIndex(j);// 选择索引 j 处的列表项
		}

        jLabel2.setText("年");
        
        for (int i = 1; i <= 12; i++) {// 利用循环，向“月”下拉列表中添加1~12
			month.addItem(String.format("%02d", i));// 格式化月份
			if (date.get(Calendar.MONTH) == i)// 当前时间月份
				month.setSelectedIndex(i - 1);// 选择索引 (i - 1) 处的列表项
		}

        jLabel3.setText("月份的销售信息，按");

        condition.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "金额", "数量" }));

        jLabel4.setText("进行");

        operation.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "升序排列", "降序排列" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(month, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addGap(6, 6, 6)
                        .addComponent(condition, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(operation, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(okButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(month, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(condition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(operation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    // 点击“确定”按钮后，发生的事件
 	private final class OkAction implements ActionListener {
 		public void actionPerformed(final ActionEvent e) {
 			List list = null;// 声明符合条件的“商品信息”集合
 			String strMonth = (String) month.getSelectedItem();// 获得被选中的月份
 			String date = year.getSelectedItem() + "-" + strMonth;// 获得含有年、月的日期
 			String con = condition.getSelectedIndex() == 0 ? "sumje " : "sl ";// 判断“条件内容”下拉列表被选中的是“金额”还是“数量”
 			int oper = operation.getSelectedIndex();// 获得“条件运算”下拉列表被选中的选项索引
 			String opstr = oper == 0 ? " asc" : " desc";// 判断“条件运算”下拉列表被选中的是“升序排列”还是“降序排列”
 			String sql1 = "select spid,sum(sl)as sl,sum(sl*dj) as sumje from v_sellView "
 						+ "where left (xsdate,7)='" + date + "' group by spid";// 获得符合年、月的销售商品信息的SQL语句
 			String queryStr = "select * from tb_spinfo s inner join (" + sql1
 							+ ") as sp on s.id=sp.spid order by " + con + opstr;// 获得销售排行的SQL语句
 			list = Dao.findForList(queryStr);// 获得符合条件的“商品信息”集合
 			Iterator iterator = list.iterator();// 获得符合条件的“商品信息”集合的迭代器
 			updateTable(iterator);// 更改表格数据
 		}
 	}
 	
 	// 更改表格数据
 	private void updateTable(Iterator iterator) {
 		DefaultTableModel dftm = (DefaultTableModel) jTable1.getModel();// 获得默认的表格模型
 		int rowCount = dftm.getRowCount();// 获得默认的表格模型中的行数
 		for (int i = 0; i < rowCount; i++) {
 			dftm.removeRow(0);// 移除表格模型中的第一行
 		}
 		while (iterator.hasNext()) {// 销售信息的集合中仍有元素可以迭代
 			Vector vector = new Vector();// 创建向量
 			List view = (List) iterator.next();// 获得迭代的下一个集合
 			Vector row = new Vector(view);// 构造一个包含指定集合view中的元素的向量
 			int rowSize = row.size();// 获得向量中的元素个数
 			for(int i = rowSize - 2; i < rowSize; i++){// 通过循环获得向量中最后两个元素的索引
 				Object colValue = row.get(i);// 获得向量中指定索引处的元素
 				row.remove(i);// 移除此向量中指定索引处的元素
 				row.insertElementAt(colValue, 2);// 将向量中指定索引处的元素插入到指定的索引为2的位置上
 			}
 			vector.addAll(row);// 将迭代后集合中所有的元素添加到此向量中
 			dftm.addRow(vector);// 向默认的表格模型中添加行数据
 		}
 	}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(XiaoShouPaiHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(XiaoShouPaiHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(XiaoShouPaiHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(XiaoShouPaiHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new XiaoShouPaiHang().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox condition;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox month;
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox operation;
    private javax.swing.JComboBox year;
    private Calendar date = Calendar.getInstance();// “日期”对象
    // End of variables declaration//GEN-END:variables
}
